unified_inventory.register_craft_type('mill', {
   description = 'Flour Mill',
   icon = 'stations_flour_mill_icon.png',
   width = 1,
   height = 1
})

unified_inventory.register_craft({
   type = 'mill',
   items = {'farming:seed_barley 4'},
   output = 'food:flour_barley'
})

unified_inventory.register_craft({
   type = 'mill',
   items = {'farming:seed_corn 4'},
   output = 'food:flour_corn'
})

unified_inventory.register_craft({
   type = 'mill',
   items = {'farming:seed_oat 4'},
   output = 'food:flour_oat'
})

unified_inventory.register_craft({
   type = 'mill',
   items = {'farming:seed_rice 4'},
   output = 'food:flour_rice'
})

unified_inventory.register_craft({
   type = 'mill',
   items = {'farming:seed_rye 4'},
   output = 'food:flour_rye'
})

unified_inventory.register_craft({
   type = 'mill',
   items = {'farming:seed_wheat 4'},
   output = 'food:flour_wheat'
})

stations.flour_seeds = {}
stations.flour_seeds['farming:seed_barley'] = true
stations.flour_seeds['farming:seed_corn'] = true
stations.flour_seeds['farming:seed_oat'] = true
stations.flour_seeds['farming:seed_rice'] = true
stations.flour_seeds['farming:seed_rye'] = true
stations.flour_seeds['farming:seed_wheat'] = true

local formspec =
   'size[8,8.5]'..
   'list[current_name;input;.5,0;1,1;]'..
   'list[current_name;output;5.5,2;2,2;]'..
   'list[current_player;main;0,4.5;8,4;]'..
   'textarea[.5,3.5;5.5,2.25;;;Four seeds will make flour]'..
   'image[2,.5;3,3;stations_flour_mill_icon.png]'..
   'listring[context;output]'..
   'listring[current_player;main]'..
   'listring[context;input]'

minetest.register_node('stations:flour_mill', {
   description = 'Flour Mill',
   drawtype = 'mesh',
   mesh = 'stations_flour_mill.obj',
   tiles = {'stations_flour_mill.png'},
   use_texture_alpha = 'opaque',
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   groups = {oddly_breakable_by_hand = 1, choppy=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('input', 1)
      inv:set_size('output', 2*2)
      meta:set_string('infotext', 'Flour Mill')
      meta:set_string('formspec', formspec)
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('input') and inv:is_empty('output') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local input = stack:get_name()
      if listname == 'input' then
         if stations.flour_seeds[input] then
            return 99
         else
            return 0
         end
      elseif listname == 'output' then
         return 0
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local timer = minetest.get_node_timer(pos)
      timer:start(3)
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local input = inv:get_stack('input', 1)
      local input_count = input:get_count()
      if input_count >= 4 then
         local output = inv:get_stack('output', 1)
         local grain_name = input:get_name()
         local grain = string.sub(grain_name, 14,-1)
         inv:add_item('output', 'food:flour_'..grain)
         input:take_item(4)
         inv:set_stack('input',1,input)
         timer:start(3)
      end
   end,
})

minetest.register_node('stations:flour_mill_locked', {
   description = 'Flour Mill (locked)',
   drawtype = 'mesh',
   mesh = 'stations_flour_mill.obj',
   tiles = {'stations_flour_mill.png'},
   use_texture_alpha = 'opaque',
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, .5, .5, .5}},
   groups = {oddly_breakable_by_hand = 1, choppy=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('input', 1)
      inv:set_size('output', 2*2)
      meta:set_string('infotext', 'Flour Mill')
      meta:set_string('formspec', formspec)
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('input') and inv:is_empty('output') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local player_name = player:get_player_name()
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
         return 0
      else
         local input = stack:get_name()
         if listname == 'input' then
            if stations.flour_seeds[input] then
               return 99
            else
               return 0
            end
         elseif listname == 'output' then
            return 0
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local timer = minetest.get_node_timer(pos)
      timer:start(3)
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local input = inv:get_stack('input', 1)
      local input_count = input:get_count()
      if input_count >= 4 then
         local output = inv:get_stack('output', 1)
         local grain_name = input:get_name()
         local grain = string.sub(grain_name, 14,-1)
         inv:add_item('output', 'food:flour_'..grain)
         input:take_item(4)
         inv:set_stack('input',1,input)
         timer:start(3)
      end
   end,
   allow_metadata_inventory_take = function(pos, listname, index, stack, player)
      local player_name = player:get_player_name()
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
         return 0
      else
         return 99
      end
   end,
})
